package wpmp.auth.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import wcommons.httputils.ResponseUtils;
import wcommons.httputils.quickbean.RespInfo;
import wpmp.auth.service.UserService;
import wpmp.utils.quickbean.User;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 2:59:34 PM Mar 25, 2013
 */
@Controller
@RequestMapping("/user.do")
public class UserController {

	private static final Logger LOG = LoggerFactory.getLogger(UserController.class);

	private UserService service;

	@Autowired
	public void setService(UserService service) {
		this.service = service;
	}

	/**
	 * <pre>
	 * 获取用户信息
	 * </pre>
	 * 
	 * @param req
	 * @param resp
	 * @param loginName
	 */
	@RequestMapping(params = "action=get", method = { RequestMethod.POST })
	public void get(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "loginName") String loginName) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("[{}] access to get User by [{}]", req.getRemoteAddr(), loginName);
		}

		final User result = service.getByLoginName(loginName);

		final RespInfo<User> respInfo;
		if (result == null) {
			respInfo = new RespInfo<User>(404);
		}
		else {
			respInfo = new RespInfo<User>(result);
		}

		ResponseUtils.renderJson(resp, respInfo.toString());
	}

	/**
	 * <pre>
	 * 用户登录
	 * 400 - 用户名或密码不正确
	 * </pre>
	 * 
	 * @param req
	 * @param resp
	 * @param loginName 用户名
	 * @param password 密码，base64编码
	 * @param expDays 失效天数
	 */
	@RequestMapping(params = "action=login", method = { RequestMethod.POST })
	public void login(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "loginName", required = true) String loginName,
			@RequestParam(value = "password", required = true) String password,
			@RequestParam(value = "expDays", required = false) Integer expDays) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("[{}] access to login by [{}, {}, {}]", req.getRemoteAddr(), loginName, password, expDays);
		}

		final User result = service.login(loginName, password, expDays);

		final RespInfo<User> respInfo;
		if (result == null) {
			respInfo = new RespInfo<User>(400);
		}
		else {
			respInfo = new RespInfo<User>(result);
		}

		ResponseUtils.renderJson(resp, respInfo.toString());
	}

	@RequestMapping(params = "action=check", method = { RequestMethod.POST })
	public void check(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "securityId", required = true) String securityId) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("[{}] access to check User by [{}]", req.getRemoteAddr(), securityId);
		}

		final boolean isLogin = service.check(securityId);

		final RespInfo<Object> respInfo;
		if (isLogin) {
			respInfo = new RespInfo<Object>(200);
		}
		else {
			respInfo = new RespInfo<Object>(400);
		}

		ResponseUtils.renderJson(resp, respInfo.toString());
	}

	/**
	 * 修改密码
	 * 
	 * @param req
	 * @param resp
	 * @param loginName
	 * @param oldPassword base64编码
	 * @param newPassword base64编码
	 * @param securityId
	 */
	@RequestMapping(params = "action=updatePassword", method = { RequestMethod.POST })
	public void updatePassword(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "loginName", required = true) String loginName,
			@RequestParam(value = "oldPassword", required = true) String oldPassword,
			@RequestParam(value = "newPassword", required = true) String newPassword,
			@RequestParam(value = "securityId", required = true) String securityId) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("[{}] access to update Password by [{}, {}, {}, {}]", req.getRemoteAddr(), loginName,
					oldPassword, newPassword, securityId);
		}

		final int code = service.updatePassword(loginName, oldPassword, newPassword, securityId);
		ResponseUtils.renderJson(resp, new RespInfo<Object>(code).toString());
	}

	@RequestMapping(params = "action=logout", method = { RequestMethod.POST })
	public void logout(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "loginName", required = true) String loginName,
			@RequestParam(value = "securityId", required = true) String securityId) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("[{}] access to logout by [{}, {}]", req.getRemoteAddr(), loginName, securityId);
		}

		final boolean result = service.logout(loginName, securityId);

		final RespInfo<Object> respInfo;
		if (result) {
			respInfo = new RespInfo<Object>(200);
		}
		else {
			respInfo = new RespInfo<Object>(400);
		}

		ResponseUtils.renderJson(resp, respInfo.toString());
	}
}
